<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="farmName">
              <a-input v-model="model.farmName" placeholder="请输入姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="耕地面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tillageArea">
              <a-input-number v-model="model.tillageArea" placeholder="请输入耕地面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="组名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupName">
              <a-input v-model="model.groupName" placeholder="请输入组名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag @change="statusChange" v-model="model.status" dict-code="fee_status" placeholder="请选择状态"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="水费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterFee">
              <a-input-number v-model="model.waterFee" placeholder="请输入水费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="已收水费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveWaterFee">
              <a-input-number v-model="model.receiveWaterFee" placeholder="请输入已收水费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="保险" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insuranceFee">
              <a-input-number v-model="model.insuranceFee" placeholder="请输入保险" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="已收保险" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveInsuranceFee">
              <a-input-number v-model="model.receiveInsuranceFee" placeholder="请输入已收保险" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="服务费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serveFee">
              <a-input-number v-model="model.serveFee" placeholder="请输入服务费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="已收服务费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveServeFee">
              <a-input-number v-model="model.receiveServeFee" placeholder="请输入已收服务费" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">
            <a-form-model-item label="contractList" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractListString">
              <a-input v-model="model.contractListString" placeholder="请输入contractList" ></a-input>
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'VcaFarmlandFeeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           year: [
              { required: true, message: '请输入年度!'},
           ],
           farmCode: [
              { required: true, message: '请输入农户编码!'},
           ],
           farmName: [
              { required: true, message: '请输入姓名!'},
           ],
           tillageArea: [
              { required: true, message: '请输入耕地面积!'},
           ],
           waterFee: [
              { required: true, message: '请输入水费!'},
           ],
           insuranceFee: [
              { required: true, message: '请输入保险!'},
           ],
           serveFee: [
              { required: true, message: '请输入服务费!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ]
        },
        url: {
          add: "/villageCadreAssist/vcaFarmlandFee/add",
          edit: "/villageCadreAssist/vcaFarmlandFee/edit",
          queryById: "/villageCadreAssist/vcaFarmlandFee/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      statusChange(value){
        if(value == 1) {
          this.model.receiveWaterFee = 0;
          this.model.receiveInsuranceFee = 0;
          this.model.receiveServeFee = 0;
        }else if(value == 3){
          this.model.receiveWaterFee = this.model.waterFee;
          this.model.receiveInsuranceFee = this.model.insuranceFee;
          this.model.receiveServeFee = this.model.serveFee;
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }

            if(this.model.receiveWaterFee ==0 &&
              this.model.receiveInsuranceFee == 0 &&
              this.model.receiveServeFee == 0 ){
              this.model.status = 1;
            }else if(this.model.receiveWaterFee != this.model.waterFee ||
              this.model.receiveInsuranceFee != this.model.insuranceFee ||
              this.model.receiveServeFee != this.model.serveFee){
              this.model.status = 2;
            }

            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      handleCategoryChange(value,backObj){
         this.model = Object.assign(this.model, backObj);
      }
    }
  }
</script>